package com.syn.synold4s.activity.setting;

import static com.gaoyi.basemodule.baseUtil.OKHttpUtil.BASE_NET_URL;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import androidx.activity.result.ActivityResult;
import com.gaoyi.basemodule.baseUtil.ActivityUtil;
import com.gaoyi.basemodule.baseUtil.AppUtil;
import com.gaoyi.basemodule.baseUtil.LanguageUtils;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.baseUtil.WindowUtil;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.syn.synold4s.R;
import com.syn.synold4s.activity.LoginActivity;
import com.syn.synold4s.activity.ProjectSelectionActivity;
import com.syn.synold4s.activity.base.BaseAlertActivity;
import com.syn.synold4s.control.ClearCacheDialog;
import com.syn.synold4s.databinding.ActivitySettingBinding;
import com.syn.synold4s.service.CloseService;
import com.syn.synold4s.util.NotificationUtil;
import com.syn.synold4s.vm.SettingViewModel;
import java.util.Map;

public class SettingActivity extends BaseAlertActivity<ActivitySettingBinding, SettingViewModel> {
    
    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        WindowUtil.setWindowDecorView(this);
        mBinding.settingToolbar.setOnBackListener(this::finish);
        mBinding.viewVersion.setContent("V" + AppUtil.getVersion());
        mBinding.viewVersion.setOnClickListener(view -> ToastUtil.showToast(R.string.cur_version));
        mBinding.viewToSystem.setOnClickListener(view -> toSetting());
        mBinding.viewLanguage.setContentId(LanguageUtils.getInstance().getStrLanguage());
        mBinding.viewLanguage.setOnClickListener(view -> startActivity(new Intent(this, LanguageActivity.class)));
        mBinding.tvPrivacy.setOnClickListener(view -> startActivity(new Intent(this, PrivacyActivity.class)));
        mBinding.tvAbout.setOnClickListener(view -> startActivity(new Intent(this, AboutUsActivity.class)));
        mBinding.tvChangeProject.setOnClickListener(view -> jumpToProjectSelection());
        mBinding.btnLogout.setOnClickListener(view -> new ClearCacheDialog(mContext, getString(R.string.confirm_logout), getString(R.string.cancel), getString(R.string.confirm), v -> {
            SPUtil.getInstance().putString(SPName.TOKEN, "");
            SPUtil.getInstance().putString(SPName.NEW_TOKEN, "");
            SPUtil.getInstance().putBoolean(SPName.LOGIN_FLAG, false);
            SPUtil.getInstance().putInt(SPName.JUMP_FLAG, -1);
            SPUtil.getInstance().putInt(SPName.REFRESH_TOKEN, 0);
            SPUtil.getInstance().putString(SPName.API_URL, BASE_NET_URL);
            NotificationUtil.getInstance().getNotificationManager().cancelAll();
            stopService(new Intent(this, CloseService.class));
            ActivityUtil.getInstance().logout();
            Intent intent = new Intent(AppUtil.getContext(), LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppUtil.getContext().startActivity(intent);
        }).show(getSupportFragmentManager()));
    }

    private void jumpToProjectSelection() {
        SPUtil.getInstance().putInt(SPName.JUMP_FLAG, 1);
        SPUtil.getInstance().putBoolean(SPName.TOKEN_FLAG, false);
        startActivity(new Intent(this, ProjectSelectionActivity.class));
    }

    private void toSetting() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivity(intent);
    }

    @Override
    protected int initVariable() {
        return 0;
    }

    @Override
    protected void initLiveData() {

    }

    @Override
    protected void initData() {

    }
}